﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace LdExpress
{
    public class PriceAreasNew
    {
        public long ID = -1;
        public string DC20 = "";
        public string DC40 = "";
        public string HC40 = "";
        public string RF20 = "";
        public string RF40 = "";
        public string Validity = "";
        public string Note = "";
        public string ValidityEx = "";
        public string NoteEx = "";
        public long CarrID = 0;
        public long PolID = 0;
        public long PodID = 0;
        public long ServiceAreaID = 0;
        public string CarrName = "";
        public string PolName = "";
        public string PodName = "";
        public string ServiceAreaName = "";
        public string ServiceAreaNameSource = "";
        public string ServiceAreaCodeSource = "";
        public string PodIncluded = "";
        public DateTime LastUpdate = DateTime.Now;
        public int Sort = 0;
        public long UserID = 0;

        public PriceAreasNew()
        {
        }

        public PriceAreasNew(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_priceareasNew";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "Insert into " + strTableName + " (" +
                             "`DC20`" +
                             ",`DC40`" +
                             ",`HC40`" +
                             ",`RF20`" +
                             ",`RF40`" +
                             ",`Validity`" +
                             ",`Note`" +
                             ",`ValidityEx`" +
                             ",`NoteEx`" +
                             ",`ServiceAreaID`" +
                             ",`CarrID`" +
                             ",`PolID`" +
                             ",`PodID`" +
                             ",`Sort`" +
                             ",`UserID`" +
                             ",`LastUpdate`" +
                             ",`ServiceAreaName`" +
                             ",`PodIncluded`" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(DC20) + "'" +
                             "," + "'" + DBModule.RefineString(DC40) + "'" +
                             "," + "'" + DBModule.RefineString(HC40) + "'" +
                             "," + "'" + DBModule.RefineString(RF20) + "'" +
                             "," + "'" + DBModule.RefineString(RF40) + "'" +
                             "," + "'" + DBModule.RefineString(Validity) + "'" +
                             "," + "'" + DBModule.RefineString(Note) + "'" +
                             "," + "'" + DBModule.RefineString(ValidityEx) + "'" +
                             "," + "'" + DBModule.RefineString(NoteEx) + "'" +
                             "," + ServiceAreaID.ToString() +
                             "," + CarrID.ToString() +
                             "," + PolID.ToString() +
                             "," + PodID.ToString() +
                             "," + Sort.ToString() +
                             "," + UserID.ToString() +
                             "," + DBModule.RefineDatetime(LastUpdate) +
                             "," + "'" + DBModule.RefineString(ServiceAreaName) + "'" +
                             "," + "'" + DBModule.RefineString(PodIncluded) + "'" +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("PriceAreasNew", "add", cn, trans);                                
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "DC20='" + DBModule.RefineString(DC20) + "'" +
                             ",DC40='" + DBModule.RefineString(DC40) + "'" +
                             ",HC40='" + DBModule.RefineString(HC40) + "'" +
                             ",RF20='" + DBModule.RefineString(RF20) + "'" +
                             ",RF40='" + DBModule.RefineString(RF40) + "'" +
                             ",Validity='" + DBModule.RefineString(Validity) + "'" +
                             ",Note='" + DBModule.RefineString(Note) + "'" +
                             ",ValidityEx='" + DBModule.RefineString(ValidityEx) + "'" +
                             ",NoteEx='" + DBModule.RefineString(NoteEx) + "'" +
                             ",ServiceAreaID = " + ServiceAreaID.ToString() +
                             ",CarrID = " + CarrID.ToString() +
                             ",PolID = " + PolID.ToString() +
                             ",PodID = " + PodID.ToString() +
                             ",Sort = " + Sort.ToString() +
                             ",UserID = " + UserID.ToString() +
                             ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "" +
                             ",ServiceAreaName='" + DBModule.RefineString(ServiceAreaName) + "'" +
                             ",PodIncluded='" + DBModule.RefineString(PodIncluded) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("PriceAreasNew", "edit", cn, trans);                
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_priceareasNew WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("PriceAreasNew", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT tbl_priceareasNew.*,POL.Name as POLName,CARR.Name as CarrName,SerAreas.Name as ServiceAreaNameSource,SerAreas.Code as ServiceAreaCodeSource " +
          " FROM tbl_priceareasNew " +
          " LEFT JOIN tbl_ports POL ON POL.ID=tbl_priceareasNew.PolID " +
          " LEFT JOIN tbl_carrs CARR ON CARR.ID=tbl_priceareasNew.CarrID " +
          " LEFT JOIN tbl_ServiceAreas SerAreas ON SerAreas.ID=tbl_priceareasNew.ServiceAreaID " +
          " WHERE tbl_priceareasNew.ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("DC20")) DC20 = dr["DC20"].ToString();
                if (!dr.IsNull("DC40")) DC40 = dr["DC40"].ToString();
                if (!dr.IsNull("HC40")) HC40 = dr["HC40"].ToString();
                if (!dr.IsNull("RF20")) RF20 = dr["RF20"].ToString();
                if (!dr.IsNull("RF40")) RF40 = dr["RF40"].ToString();
                if (!dr.IsNull("Validity")) Validity = dr["Validity"].ToString();
                if (!dr.IsNull("Note")) Note = dr["Note"].ToString();
                if (!dr.IsNull("ValidityEx")) ValidityEx = dr["ValidityEx"].ToString();
                if (!dr.IsNull("NoteEx")) NoteEx = dr["NoteEx"].ToString();
                if (!dr.IsNull("ServiceAreaID")) ServiceAreaID = long.Parse(dr["ServiceAreaID"].ToString());
                if (!dr.IsNull("CarrID")) CarrID = long.Parse(dr["CarrID"].ToString());
                if (!dr.IsNull("PolID")) PolID = long.Parse(dr["PolID"].ToString());
                if (!dr.IsNull("PodID")) PodID = long.Parse(dr["PodID"].ToString());
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("UserID")) UserID = int.Parse(dr["UserID"].ToString());
                if (!dr.IsNull("PolName")) PolName = dr["PolName"].ToString();
                //if (!dr.IsNull("PodName")) PodName = dr["PodName"].ToString();
                if (!dr.IsNull("CarrName")) CarrName = dr["CarrName"].ToString();
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("ServiceAreaName")) ServiceAreaName = dr["ServiceAreaName"].ToString();
                if (!dr.IsNull("ServiceAreaNameSource")) ServiceAreaNameSource = dr["ServiceAreaNameSource"].ToString();
                if (!dr.IsNull("ServiceAreaCodeSource")) ServiceAreaCodeSource = dr["ServiceAreaCodeSource"].ToString();
                if (!dr.IsNull("PodIncluded")) PodIncluded = dr["PodIncluded"].ToString();
            }
        }
        public static void GetList(string condition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "SELECT tbl_priceareasNew.*,POL.Name as POLName,CARR.Name as CarrName" +
            ",SerAreas.Name as ServiceAreaNameSource,SerAreas.Code as ServiceAreaCodeSource " +
           " FROM tbl_priceareasNew " +
           " LEFT JOIN tbl_ports POL ON POL.ID=tbl_priceareasNew.PolID " +
           " LEFT JOIN tbl_carrs CARR ON CARR.ID=tbl_priceareasNew.CarrID " +
           " LEFT JOIN tbl_ServiceAreas SerAreas ON SerAreas.ID=tbl_priceareasNew.ServiceAreaID " +
           " WHERE 1=1";
            if (condition.Length > 0)
            {
                strSQL += condition;
            }

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            //GV.Response.Write(strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_priceareasNew where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }

    }
}
